<?php

namespace ContentOutputBundle\Controller;


use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Yaml\Yaml;


/**
 * Class DefaultController
 * @package ContentOutputBundle\Controller
 * @Route("/v1.0")
 */
class DefaultController extends Controller
{
    /**
     * @Route("/book_info")
     * 书籍基本信息
     */
    public function bookInfo()
    {
        $response = new Response();
        $request = Request::createFromGlobals();
        $book_id = $request->query->get("book_id");
        $result = [];
        if (intval(abs($book_id))) {
            $book_info = $this->get("content_output.cache_data_v1")
                ->getbookCacheData($book_id);
            if ($book_info) {
                $result["data"] = $book_info;
                $result["code"] = 200;
                $result["msg"] = "success";

            } else {
                $result["data"] = [];
                $result["code"] = 404;
                $result["msg"] = "book no exist";
            }
        } else {
            $result["data"] = [];
            $result["code"] = 400;
            $result["msg"] = " book_id error";
        }
        $response->headers->set('Content-Type', "application/json;charset=UTF-8");
        $response->setStatusCode($result["code"]);
        $response->setContent(json_encode($result, JSON_UNESCAPED_UNICODE));
        return $response;
    }

    /**
     * @Route("/book_chapter_info")
     * 书籍章节信息
     */
    public function bookChapterInfo()
    {
        $response = new Response();
        $request = Request::createFromGlobals();
        $book_id = $request->query->get("book_id");
        $chapter_id = $request->query->get("chapter_id");

        if (intval(abs($book_id)) && intval(abs($chapter_id))) {

            $redis_config = Yaml::parse(file_get_contents(__DIR__ . '/../Resources/config/redis_config.yml'));
            $redis = $this->get("snc_redis.default_client");
            $redis_data_expire_time = $redis_config["data_expire"];
            $redis_key = "book_chapter_info:book_id:" . $book_id . ":chapter_id:" . $chapter_id;

            $redis_data = $redis->get($redis_key);
            if (!$redis_data) {
                $chapter = $this->get("content_output.chapter_v1")->getBookChapter($book_id, $chapter_id);
                if ($chapter) {
                    $result["data"] = $chapter;
                    $result["code"] = 200;
                    $result["msg"] = "success";
                    $redis->set($redis_key, json_encode($chapter, JSON_UNESCAPED_UNICODE));
                    $redis->expire($redis_key, $redis_data_expire_time);
                } else {
                    $result["data"] = [];
                    $result["code"] = 404;
                    $result["msg"] = "chapter no existent";
                }
            } else {
                $result["data"] = json_decode($redis_data, true);
                $result["code"] = 200;
                $result["msg"] = "success";
            }

        } else {
            $result["data"] = [];
            $result["code"] = 400;
            $result["msg"] = " book_id or chpater_id error";
        }
        $response->headers->set('Content-Type', "application/json;charset=UTF-8");
        $response->setStatusCode($result["code"]);
        $response->setContent(json_encode($result, JSON_UNESCAPED_UNICODE));
        return $response;
    }

    /**
     * @Route("/book_chapter_list")
     * 书籍章节列表
     */
    public function bookChapterList()
    {

        $request = Request::createFromGlobals();
        $book_id = $request->query->get("book_id");
        $book_id = intval(abs($book_id));


        $result = [
            "code" => 404,
            "data" => [],
            "msg" => "no data"
        ];

        if ($book_id) {
            $book_chapter_list = $this->get("content_output.cache_data_v1")->getBookChapterListCacheData($book_id);
            if ($book_chapter_list) {
                $result["code"] = 200;
                $result["msg"] = "success";
                $result["data"] = $book_chapter_list;
            }
        } else {
            $result["data"] = [];
            $result["code"] = 400;
            $result["msg"] = " book_id error";
        }
        $response = new Response();
        $response->headers->set('Content-Type', "application/json;charset=UTF-8");
        $response->setStatusCode($result["code"]);
        $response->setContent(json_encode($result, JSON_UNESCAPED_UNICODE));
        return $response;
    }

    /**
     * @Route("/book_chapter_content")
     * 书籍章节内容
     */
    public function bookChapterContent()
    {
        $response = new Response();
        $request = Request::createFromGlobals();
        $book_id = $request->query->get("book_id");
        $chapter_id = $request->query->get("chapter_id");

        if (intval(abs($book_id)) && intval(abs($chapter_id))) {

            $book_chapter_content= $this->get("content_output.cache_data_v1")
                ->getBookChpaterContentCacheData($book_id, $chapter_id);

            if ($book_chapter_content["code"]==200) {
                $result["data"] = $book_chapter_content["data"];
                $result["code"] = 200;
                $result["msg"] = "success";
            } else {
                $result["data"] = [];
                $result["code"] = 404;
                $result["msg"] = $book_chapter_content["msg"];
            }
        }else {
            $result["data"] = [];
            $result["code"] = 400;
            $result["msg"] = " book_id or chapter_id error";
        }
        $response->headers->set('Content-Type', "application/json;charset=UTF-8");
        $response->setStatusCode($result["code"]);
        $response->setContent(json_encode($result, JSON_UNESCAPED_UNICODE));
        return $response;

    }

    /**
     * @Route("/book_category")
     */
    public function bookCategory()
    {
        $response = new Response();
        $request = Request::createFromGlobals();
        $result = $this->get("content_output.book_v1")->getBookCategory();

        $response->headers->set('Content-Type', "application/json;charset=UTF-8");

        $response->setContent(json_encode($result, JSON_UNESCAPED_UNICODE));
        return $response;
    }
}
